package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Goods;
import cdu.javaendwork.model.sqlModel.Items;
import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.service.Impl.Shopping.GoodsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.ItemsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.OrderServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.GoodsService;
import cdu.javaendwork.service.Interface.Shopping.ItemsService;
import cdu.javaendwork.service.Interface.Shopping.OrderService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/order_show")
public class OrderShowController extends HttpServlet {
    OrderService orderService = new OrderServiceImpl();
    GoodsService goodsService = new GoodsServiceImpl();
    ItemsService itemsService = new ItemsServiceImpl();
    PageHelper pageHelper = new PageHelper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //拿到页码
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);
        //拿到要展示的订单id
        String oid = req.getParameter("orderid");
        int orderid = Integer.parseInt(oid);

        //先查找到该订单
        Order order = orderService.findById(orderid);
        //根据order查到所有该订单的条目
        List<Items> itemsList = itemsService.filterByOrderIdLimit(orderid,pageIndex,pageSize);
        //查询该集合的分页信息
        PageInfo pageInfo = pageHelper.filter("Items","orderid="+orderid,pageIndex,pageSize);
        //获取每个条目对应的商品信息
        List<Goods> goodsList = new ArrayList<>();
        for(Items items:itemsList)
        {
            int goodid = items.getGoodid();
            Goods goods = goodsService.findById(goodid);
            goodsList.add(goods);
        }
        //将得到的信息丢到页面去
        req.setAttribute("order",order);
        req.setAttribute("goodsList",goodsList);
        req.setAttribute("itemsList",itemsList);
        req.setAttribute("pageInfo",pageInfo);

        req.getRequestDispatcher("orderShow.jsp").forward(req,resp);
    }
}
